﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.Infastructure;
using Toolkit.Web.ExtensionMethods;

namespace Toolkit.Web.ViewComponents.Button
{
    public sealed class ButtonMenuBuilder
    {
        private ButtonMenu _buttonMenu;

        public ButtonMenuBuilder(ButtonMenu component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }

            _buttonMenu = component;
        }

        public ButtonMenuBuilder HtmlAttributes(object htmlAttributes)
        {
            if (htmlAttributes != null)
            {
                _buttonMenu.HtmlAttributes.Merge(htmlAttributes, true);
            }

            return this;
        }

        public ButtonMenuBuilder CustomMenuSection(Action<object, IViewComponentWriter> template)
        {
            if (template != null)
            {
                if (_buttonMenu.OverrideTemplate == null)
                {
                    _buttonMenu.OverrideTemplate = new HtmlTemplate();
                }

                _buttonMenu.OverrideTemplate.ActionTemplate = template;
            }
            else
            {
                _buttonMenu.OverrideTemplate = null;
            }

            return this;
        }

        public ButtonMenuBuilder CustomMenuSection(Func<object, object> template)
        {
            if (template != null)
            {
                if (_buttonMenu.OverrideTemplate == null)
                {
                    _buttonMenu.OverrideTemplate = new HtmlTemplate();
                }

                _buttonMenu.OverrideTemplate.InlineTemplate = template;
            }
            else
            {
                _buttonMenu.OverrideTemplate = null;
            }

            return this;
        }

        public ButtonMenuBuilder AddMenuSection(Action<ButtonMenuSectionBuilder> action)
        {
            if (action != null)
            {
                ButtonMenuSection section = new ButtonMenuSection();
                ButtonMenuSectionBuilder sectionBuilder = new ButtonMenuSectionBuilder(section);
                action(sectionBuilder);
                _buttonMenu.Sections.Add(section);
                _buttonMenu.OverrideTemplate = null;
            }

            return this;
        }
    }
}
